// pages/mine/personalInformation/deliveryAddress/deliveryAddress.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList: [],
    type:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type:options.type
    })
    this.getUserAddressList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserAddressList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getUserAddressList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //获取收货地址列表
  getUserAddressList:function(){
    let self=this
    wx.showLoading({
      title: '正在加载...',
    })
    wx.request({
      url: wx.envConfig.host + '/user/findUserAddressByUserId?params=' + JSON.stringify({ user_id: wx.getStorageSync('userInfo').id}) + '&cookiez=' + wx.getStorageSync('cookiez'),
      method: 'POST',
      success: function (ress) {
        if (ress.data.error_code === "Y10000") {
          
          self.setData({
            addressList:ress.data.datas.map(it => {
                it.scrollLeft = 0
                return it
              })
          })
        }
        wx.hideLoading();
        wx.stopPullDownRefresh();
      },
      fail: function ({ errMsg }) {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        console.log('request fail', errMsg)
      }
    })
  },
  /**
   * 编辑地址
   */
  editAddress: function (e) {
    // console.log('event````edit', e)
    let item = e.currentTarget.dataset.item
    wx.setStorageSync('editAddress',item)
    wx.navigateTo({
      url: './editAddress/editAddress?type=edit',
    })
    // e.stopPropagation
  },
  /**
   * 删除地址
   */
  deleteAddress: function (e) {
    console.log('delete', e)
    var self = this
    var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    wx.showModal({
      title: '提示',
      content: '确定要删除该地址吗？',
      cancelText: '否',
      cancelColor: '#4825BF',
      confirmText: '是',
      confirmColor: '#4825BF',
      success: function(res) {
        if (res.confirm) {
          self.data.addressList.splice(index, 1);
          self.deleteUserAddress(id)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //删除地址
  deleteUserAddress: function (addressId) {
    let self = this
    wx.request({
      url: wx.envConfig.host + '/user/deleteUserAddressById?params=' + JSON.stringify({ id:addressId}) + '&cookiez=' + wx.getStorageSync('cookiez'),
      method: 'POST',
      success: function (ress) {
        if (ress.data.error_code === "Y10000") {
          self.getUserAddressList()
        }
      },
      fail: function ({ errMsg }) {
        console.log('request fail', errMsg)
      }
    })
  },
  /**
   * 点击添加新地址
   */
  addAddress: function () {
    wx.navigateTo({
      url: './editAddress/editAddress?type=add',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  // 选择地址
  chooseAddress:function(event){
    if (this.data.type=='edit'){
      wx.setStorageSync('address', event.currentTarget.dataset.item)
      wx.navigateBack()
    }
  }
})